//
// Created by WenDaoJiang on 2017/6/21.
// Copyright (c) 2017 文道江. All rights reserved.
//

#import "AlertView.h"


@implementation AlertView {
    NSMutableDictionary *_callbacks;
    NSInteger index;
}

+ (AlertView *)instance {
    static AlertView *_instance = nil;

    @synchronized (self) {
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }

    return _instance;
}

+ (void)confirmWithTitle:(NSString *)title Message:(NSString *)message Buttons:(NSArray *)buttons CallBack:(AlertCallBack)callback {
    [self.instance confirmWithTitle:title Message:message Buttons:buttons CallBack:callback];
}

+ (void)confirmWithTitle:(NSString *)title Message:(NSString *)message CallBack:(AlertCallBack)callback {
    [self.instance confirmWithTitle:title Message:message CallBack:callback];
}

+ (void)alertWithTitle:(NSString *)title Message:(NSString *)message Callback:(AlertCallBack)callback {
    [self.instance alertWithTitle:title Message:message Callback:callback];
}

+ (void)alertWithTitle:(NSString *)title Message:(NSString *)message ButtonText:(NSString *)buttonText Callback:(AlertCallBack)callback {
    [self.instance alertWithTitle:title Message:message ButtonText:buttonText Callback:callback];
}


- (instancetype)init {
    self = [super init];
    if (self) {
        _callbacks = [[NSMutableDictionary alloc] init];
        index = 0;
    }

    return self;
}


- (void)confirmWithTitle:(NSString *)title Message:(NSString *)message Buttons:(NSArray *)buttons CallBack:(AlertCallBack)callback {
    if(_callbacks!=nil){
        [_callbacks setValue:callback forKey:[NSString stringWithFormat:@"%i", index]];
    }
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:buttons[0]
                                              otherButtonTitles:buttons[1], nil];
    alertView.tag = index;
    [alertView show];
    index++;
}

- (void)confirmWithTitle:(NSString *)title Message:(NSString *)message CallBack:(AlertCallBack)callback {
    [self confirmWithTitle:title Message:message Buttons:@[@"取消", @"确定"] CallBack:callback];
}

- (void)alertWithTitle:(NSString *)title Message:(NSString *)message ButtonText:(NSString *)buttonText Callback:(AlertCallBack)callback {
    if(_callbacks!=nil){
        [_callbacks setValue:callback forKey:[NSString stringWithFormat:@"%i", index]];
    }
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle: buttonText
                                              otherButtonTitles:nil, nil];
    alertView.tag = index;
    [alertView show];
    index++;
}

- (void)alertWithTitle:(NSString *)title Message:(NSString *)message Callback:(AlertCallBack)callback {
    [self alertWithTitle:title Message:message ButtonText:@"确定" Callback:callback];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *key = [NSString stringWithFormat:@"%i", alertView.tag];
    AlertCallBack callback = _callbacks[key];
    if(callback != nil){
        callback(buttonIndex);
        [_callbacks removeObjectForKey:key];
    }
}

@end